from logging import info, debug, error
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import os
from google.appengine.ext.webapp import template
from models import Feed, FeedContent, UserFeeds


class MainPage(webapp.RequestHandler):
  def get(self):
    #self.response.headers['Content-Type'] = 'text/plain'
    
    user = users.get_current_user()

    if user:
		# Fetch feeds
		uf = UserFeeds.all(  ).filter( "user = ", user ).fetch( 1 )

		if len( uf ) == 0: 

			template_values = {
			}

			if self.request.get("notparsed"):
				template_values["notparsed"] = """
					Error parsing file. File format must be OPML
				"""

			path = os.path.join(os.path.dirname(__file__), 'start.html')
			result = template.render(path, template_values)
			self.response.out.write(result)

			
		else:
			self.redirect( "/show" )	

    else:
            self.redirect(users.create_login_url(self.request.uri))


application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)



def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
  
